import { defineStore } from 'pinia';

const useBluetoothStore = defineStore("bluetooth", {
	state: () : any => ({
		device: null,					// 当前连接的蓝牙设备ID
		AdopterInit: false,				// 蓝牙模块是否初始化
		bluetoothConnection: false,		// 低功耗蓝牙连接状态	
		available: false,				// 蓝牙适配器是否可用
		discovering: false,				// 是否正在搜索设备
		isBlueSetting: false,			// 是否处于蓝牙连接页面，防止多次跳转
		deviceInfo: {
			melody: "",			// 曲调
			volume: 0,		 	// 音量	 
		}
	}),
	actions: {
		updateBluetoothStatus(available : boolean, discovering : boolean) {
			if (this.available != available) {
				this.available = available;
			}
			if (this.discovering != discovering) {
				this.discovering = discovering;
			}
		},
		updateBluetoothConnection(connected : boolean) {
			this.bluetoothConnection = connected;
		},
		/**
		 * 记录当前连接到的蓝牙设备ID
		 */
		updateDeviceId(device : UniApp.BluetoothDeviceInfo) {
			// 连接历史写入缓存，下次打开APP优先连接这个
			uni.setStorageSync("historyBluetoothDevice", device);
			this.device = device
		},
		/**
		 * 记录是否在蓝牙设置页
		 */
		updatePageCurrent(isBlueSetting : boolean) {
			this.isBlueSetting = isBlueSetting;
		}
	},

})

export default useBluetoothStore;